package com.ziyueyuwen.xiniao.aop.classuser;

import com.alibaba.fastjson.JSON;
import com.ziyueyuwen.xiniao.bean.ZyClassInfo;
import com.ziyueyuwen.xiniao.bean.ZyClassLessonInfo;
import com.ziyueyuwen.xiniao.bean.ZyProductInfo;
import com.ziyueyuwen.xiniao.dao.ZyClassInfoMapper;
import com.ziyueyuwen.xiniao.dao.ZyClassUserMapper;
import com.ziyueyuwen.xiniao.service.baseservice.product.ProductInfoBaseService;
import org.aspectj.lang.JoinPoint;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wancheng on 2019/4/19.
 */
@Component
@Aspect
public class AddStudentAop {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    ZyClassUserMapper zyClassUserMapper;
    @Autowired
    ZyClassInfoMapper zyClassInfoMapper;
    @Autowired
    ProductInfoBaseService productInfoBaseService;
    /**
     * 从后台添加多个学生后对商品进行下架检测
     */
    @After(value = "execution(* com.ziyueyuwen.xiniao.service.impl.ClassUserServiceImpl.addStudent(..))")
    public void afterAddStudent(JoinPoint joinPoint) {
        checkUpperProduct(joinPoint);
    }
    /**
     * 从后台添加一个学生后对商品进行下架检测
     */
    @After(value = "execution(* com.ziyueyuwen.xiniao.service.impl.ClassUserServiceImpl.addStudentOne(..))")
    public void afterAddStudentOne(JoinPoint joinPoint) {
        checkUpperProduct(joinPoint);
    }
    /**
     * 从商品处添加学生后对商品进行下架检测
     */
    @After(value = "execution(* com.ziyueyuwen.xiniao.service.impl.ClassUserServiceImpl.addClassStudentFromProduct(..))")
    public void afterAddClassStudentFromProduct(JoinPoint joinPoint) {
        checkUpperProduct(joinPoint);
    }

    private void checkUpperProduct(JoinPoint joinPoint) {
        try {
            Object[] obj = joinPoint.getArgs();
            Integer classId = null;
            if (obj != null) {
                classId = (Integer) obj[0];
                if (classId == null) {
                    return;
                }
            }
            logger.info("检测商品的上下架状态 classId = {}", classId);
            ZyClassInfo classInfo = zyClassInfoMapper.selectByPrimaryKey(classId);
            ZyProductInfo param = new ZyProductInfo();
            param.setClassId(classId);
            ZyProductInfo product = productInfoBaseService.selectOneByZyCommodityInfo(param);
            if (product == null) {
                return;
            }
            logger.info("商品ID productId = {}", product.getId());
            Integer studentCountUpdate = zyClassUserMapper.getStudentCount(classId);
            if (studentCountUpdate.intValue() >= classInfo.getStudentNum().intValue()) {
                param.setId(product.getId());
                param.setIfUpperShelf(false);
                productInfoBaseService.update(param);
                logger.info("商品ID productId = {}, 班级 classId = {}, 班级人数 count = {} ,状态:下架", product.getId(), classId, studentCountUpdate);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
